package mvc;

import java.util.*;
import java.sql.*;
import java.util.regex.*;

public class Profile {
	
	private String username = null;
	private String name = null;
	private String firstname = null;
	private String lastname = null;
	private String email = null;
	private String profile_text = null;
	private String mobilenr = null;
	private String picture_http = null;
	private String website = null;
	private String hasphoto = null;
	private DBconn db;
	       
public Profile(DBconn db, String userprofile) throws SQLException
{
	this.db = db;
	db.sqlQuery("SELECT username,firstname,lastname,email,profile_text, picture_http, website, hasphoto, mobilenr "+
				"FROM person,profile where person.username = profile.person and profile.person='"+userprofile+"'");
	
	db.next();
	
	username = db.getString("username");
	name = db.getString("firstname") +" "+ db.getString("lastname");
	email = db.getString("email");
	profile_text = db.getString("profile_text");
	picture_http = db.getString("picture_http");
	website = db.getString("website");	
	firstname = db.getString("firstname");
	lastname = db.getString("lastname");
	hasphoto = db.getString("hasphoto");
 	mobilenr = db.getString("mobilenr");				

}

public String getUsername() { return username; }

public String getProfiletext()  throws SQLException { 

	Pattern pat = Pattern.compile("\\[friend](\\S+)\\[/friend]");
	Matcher match = pat.matcher(profile_text);
	
	while(match.find()){
	String temp = match.group(1);
	
	if(db.sqlQuery("select username from person where username ='"+temp+"'").next()){
		profile_text = profile_text.replaceFirst("\\[friend]","<a href=\"/servlet/mvc.Servl?state=profile&profile="+temp+"\">");
		profile_text = profile_text.replaceFirst("\\[/friend]","</a>");
	
	}	
	}
	
	return profile_text.replaceAll("\n","<br>"); 
	}
	
public String getProfiletextEdit() { return profile_text; }

public String getPictureHttp() { return picture_http; }

public String getEmail() { return email; }

public String getWebsite() { return website; }

public String getMobilenr() { return mobilenr; }

public String getName() { return name; }       

public String getFirstname() { return firstname; } 

public String getLastname() { return lastname; } 

public boolean hasPhoto() { return hasphoto.equals("1"); }

public String getPhoto(){
	
	if(this.hasPhoto())
		return "/mvcjsp/photos/"+this.username+".jpg";
	else
		return this.picture_http;
}





}